#"""
#wxstc.py
#Login : <manikandanp@gmail.com>
#Started on  Mon Jan  8 14:00:54 2007 Manikandan Pattabiraman
#$Id$
#"""


import sys
import keyword
from wx.stc import *


cKeywords = "asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile whileasm while"
class AddCommentDialog(wx.Dialog):
	def __init__(self,parent,id,title):
		wx.Dialog.__init__(self, parent,id,title)   

class MySTC(StyledTextCtrl):
	def __init__(self, parent, size=wx.DefaultSize, style=0):
		self.parent = parent
		self.menu = wx.Menu()
		StyledTextCtrl.__init__(self, parent, size=size, style=style)
		self.menu.Append(101, "Add Comment")
		self.menu.Append(102, "E&xit")
		wx.EVT_MENU(self , 101, self.parent.addComment)
		wx.EVT_MENU(self , 102, lambda evt: sys.exit(1))
		self.SetCaretLineBack('light blue')
		self.SetCaretLineVisible(True)
		wx.EVT_CONTEXT_MENU(self, self.OnContextMenu)

	def OnMarginClick(self,evt):
		self.PopupMenu(self.menu)
		
	def OnContextMenu(self,evt):
		self.PopupMenu(self.menu)


class AnnotateWnd(wx.Frame):
	def __init__(self):
		global MainEditor
		wx.Frame.__init__(self, None, -1, 'Mani')
		MainEditor = MySTC(self)
		sizer = wx.BoxSizer(wx.VERTICAL)
		sizer.Add(MainEditor, 1, wx.EXPAND)
		self.comments = []
		self.SetSizer(sizer)
		self.SetSize((800,600))
		self.setStyles()
		self.createMenuAndStatusBar()

	def addComment(self, comment):
		text = ''
		print MainEditor.GetSelectedText()
		dlg = AddCommentDialog(self, -1,"Add a comment")
		dlg.ShowModal()
		dlg.Destroy()       
		self.comments.append(text)

	def createMenuAndStatusBar(self):
		self.CreateStatusBar()
		self.SetStatusText("Annotate Tool")

		menu = wx.Menu()
		menu = wx.Menu()
		menu.Append(wx.ID_ABOUT, "&About",
					"More information about this program")
		menu.AppendSeparator()
		menu.Append(wx.ID_EXIT, "E&xit", "Terminate the program")
		
		menuBar = wx.MenuBar()
		menuBar.Append(menu, "&File");
		
		self.SetMenuBar(menuBar)

	def setStyles(self):
		MainEditor.SetUseHorizontalScrollBar(False)
		MainEditor.SetMarginWidth(0,35)
		MainEditor.SetUseAntiAliasing(True)

		MainEditor.StyleSetSpec(STC_STYLE_DEFAULT,
								"face:Courier,size:11")
		MainEditor.StyleClearAll()
		MainEditor.SetLexerLanguage("cpp")
		MainEditor.SetKeyWords(0,cKeywords)

		MainEditor.SetTabWidth(4)
		MainEditor.StyleSetSpec(STC_C_DEFAULT,
				"fore:#6df44f,size:%d" % 12)
		MainEditor.StyleClearAll()
		
		MainEditor.StyleSetSpec(STC_C_COMMENT,
				"fore:#7F7BBB,size:%d,italic" % 11)

		MainEditor.StyleSetSpec(STC_C_WORD,
				"fore:red,size:%d,bold" % 12)
		
		MainEditor.StyleSetSpec(STC_C_NUMBER,
				"fore:darkblue,size:%d" % 12)

		MainEditor.StyleSetSpec(STC_C_STRING,
				"fore:#7FBB7F,face:%s,size:%d" % ('Courier', 10))
		MainEditor.StyleSetSpec(STC_C_IDENTIFIER,
				"fore:darkgreen,face:%s,size:%d,bold" % ('Courier', 11))
		MainEditor.StyleSetSpec(STC_C_PREPROCESSOR,
				"fore:#12df3F,face:%s,size:%d" % ('Courier', 11))
		MainEditor.Colourise(0,MainEditor.GetLength())
		MainEditor.SetIndentationGuides(True)
		MainEditor.UsePopUp(False)
		MainEditor.autoIndent = True

	def loadDocument(self, filename):
		buffer = open(filename,'rb')
		MainEditor.AddText(buffer.read())

def main():
	app = wx.PySimpleApp()
	frame = AnnotateWnd()
	frame.Show(True)
	frame.loadDocument("./pt.c")
	app.MainLoop()
					 
if __name__ == '__main__':
	main()
